package com.StarStudio.Utils;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.Entity.UserInfo;

import java.io.File;
import java.io.RandomAccessFile;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.List;

/**
 * 生成用户工具类
 */
public class UserUtil {


    public static void createUser(int count) throws Exception {
        List<UserInfo> users = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            UserInfo user = new UserInfo();
            user.setId(18000000000L + i);
            user.setReal_name("user" + i);
            user.setCard_num("42126199700000000" + i);
            user.setCard_type(1);
            user.setMobile("189789000" + i);
            user.setHours(100.0);
            user.setBeans(1000);
            user.setStar(0);
            users.add(user);
        }
        //插入数据库
        /*
        Connection connect = getConnect();
        String sql = "insert into user_info(id,real_name,card_num,card_type,mobile,hours,beans,star) values(?,?,?,?,?,?,?,?)";
        PreparedStatement preparedStatement = connect.prepareStatement(sql);
        for (int i = 0; i < users.size(); i++) {
            UserInfo user = users.get(i);
            preparedStatement.setLong(1,user.getId());
            preparedStatement.setString(2,user.getReal_name());
            preparedStatement.setString(3,user.getCard_num());
            preparedStatement.setInt(4,user.getCard_type());
            preparedStatement.setString(5,user.getMobile());
            preparedStatement.setDouble(6, user.getHours());
            preparedStatement.setInt(7, user.getBeans());
            preparedStatement.setInt(8, user.getStar());
            preparedStatement.addBatch();
        }
        preparedStatement.executeBatch();
        preparedStatement.clearParameters();
        connect.close();
        System.out.println("insert into database");*/

        //生成用户信息文件  .txt
        File file = new File("C:\\Users\\92149\\Desktop\\user.txt");
        if (file.exists()){
            file.delete();
        }
        RandomAccessFile raf = new RandomAccessFile(file,"rw");
        raf.seek(0);
        for (UserInfo user : users) {
            String row = user.getId() + "," + user.getReal_name() + "," + user.getMobile() + "," + user.getHours() + "," + user.getBeans();
            raf.seek(raf.length());
            raf.write(row.getBytes());
            raf.write("\r\n".getBytes());
        }
        raf.close();
        System.out.println("生成用户信息文件 over");
/*
        //生成请求信息文件  .txt
        File file1 = new File("C:\\Users\\92149\\Desktop\\order.txt");
        if (file1.exists()){
            file1.delete();
        }
        RandomAccessFile raf1 = new RandomAccessFile(file1,"rw");
        raf1.seek(0);
        for (UserInfo user : users) {
            String row = user.getId() + "," + "1" + "," + "1649383484690800640" + "," + "订单请求中" + "," + user.getReal_name() + "," + user.getMobile() + "," + "建设北路二段四号" + "," + "线下厂商配送";
            raf1.seek(raf1.length());
            raf1.write(row.getBytes());
            raf1.write("\r\n".getBytes());
        }*/
        //生成请求信息文件  .txt
        File file1 = new File("C:\\Users\\92149\\Desktop\\couponsOrder.txt");
        if (file1.exists()){
            file1.delete();
        }
        RandomAccessFile raf1 = new RandomAccessFile(file1,"rw");
        raf1.seek(0);
        for (UserInfo user : users) {
            String row = user.getId() + "," + "1" + "," + "1652163351053926400" + "," + "订单请求中" + "," + user.getReal_name() + "," + user.getMobile() + "," + "建设北路二段四号" + "," + "线下厂商配送";
            raf1.seek(raf1.length());
            raf1.write(row.getBytes());
            raf1.write("\r\n".getBytes());
        }
        raf1.close();
        System.out.println("生成请求信息文件 over");

    }

    private static Connection getConnect() throws Exception{
        String url = "jdbc:mysql://112.19.80.236:9849/ry-cloud?userUnicode=ture&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "yibinmall2023";
        String driver = "com.mysql.cj.jdbc.Driver";
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    }


    public static void main(String[] args) throws Exception {
//        createUser(1000);
        System.out.println(IdUtil.getSnowflakeNextId());
    }
}
